/*表单验证*/
if(!window.verify) {
	window.verify = function(dom, blurEv) {
		var verifyErrorB = true;
		$(dom).each(function() {
			if(new RegExp(this.nodeName, 'i').test('input,textarea,select')) {
				if(this.getAttribute('verify')) {
					var _verify = this.getAttribute('verify').replace(/\s/g, '').split('|');
					var verifyT = verify.Fn(this, _verify);
					if(verifyT) {
						verify.Fn2(this, verifyT, blurEv);
						verifyErrorB = false;
						return false
					}
				}
			} else {
				$.each(this.querySelectorAll("input,textarea,select"), function() {
					if(this.getAttribute('verify')) {
						var _verify = this.getAttribute('verify').replace(/\s/g, '').split('|');
						var verifyT = verify.Fn(this, _verify);
						if(verifyT) {
							verify.Fn2(this, verifyT, blurEv);
							verifyErrorB = false;
							return false
						}
					}
				});
				return false
			}
		});
		return verifyErrorB
	};
	verify.Fn = function(_this, _verify) {
		var val = _this.value,
			verifyT;
		$.each(_verify, function(i, v) {
			if($.type(verify.rule[v]) === 'array' && !verify.rule[v][0].test(val)) {
				if(typeof verify.rule[v][1] === 'string') {
					verifyT = verify.rule[v][1]
				} else {
					verifyT = verify.rule[v][1](val, _this)
				}
				return false
			} else if(typeof verify.rule[v] === 'function') {
				verifyT = verify.rule[v](val, _this);
				if(verifyT) {
					return false
				}
			}
		});
		return verifyT
	};
	verify.Fn2 = function(_this, _verifyT, blurEv) {
		if(_this.nodeName === 'SELECT' || _this.type === 'radio' || _this.type === 'checkbox') {
			_this = _this.nextElementSibling || _this.nextSibling;
		} else if(_this.nodeName === 'INPUT' && !blurEv) {
			_this.focus()
		}
		_this.setAttribute('danger', '');
		setTimeout(function() {
			_this.removeAttribute('danger')
		}, 2000);

		switch(_this.getAttribute('verType')) {
			case 'msg':
				layer.msg(_verifyT, {
					/*icon: 2,*/
					time: 1000
				});
				break;
			case 'alert':
				layer.alert(_verifyT, {
					/*icon: 2,*/
					time: 1000
				});
				break;
			default:
				setTimeout(function() {
					layer.tips(_verifyT, _this, {
						tips: [1, '#008bff'],
						time: 2000
					})
				}, 0)
		}
	};
	verify.rule = {
		/*required: [/[\S]+/, "必填项不能为空"],*/
		required: function(v, _this) {
			if(_this.nodeName === 'SELECT') {
				if($(_this).val() === null) {
					return "必填项不能为空"
				}
			} else if(_this.type === 'radio' || _this.type === 'checkbox') {
				if(_this.checked == false) {
					return "必选项不能忽略"
				}
			} else if(!/[\S]+/.test(v)) {
				return "必填项不能为空"
			}
		},
		phone: [/^1\d{10}$|^$/, "请输入正确的手机号"],
		email: [/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$|^$/, "邮箱格式不正确"],
		url: [/(^#)|(^http(s*):\/\/[^\s]+\.[^\s]+)|^$/, "链接格式不正确"],
		number: function(e) {
			if(isNaN(e)) {
				return "只能填写数字"
			}
		},
		date: [/^(\d{4})[-\/](\d{1}|0\d{1}|1[0-2])([-\/](\d{1}|0\d{1}|[1-2][0-9]|3[0-1]))*$|^$/, "日期格式不正确"],
		identity: [/(^\d{15}$)|(^\d{17}(x|X|\d)$)|^$/, "请输入正确的身份证号"],
		minmax: function(v, d) {
			if(v.length) {
				if(d.getAttribute('minlength') && v.length < d.getAttribute('minlength')) {
					return '请输入不小于' + d.getAttribute('minlength') + '位内容'
				} else if(d.getAttribute('maxlength') && v.length > d.getAttribute('maxlength')) {
					return '请输入不大于' + d.getAttribute('minlength') + '位内容'
				}
			}
		}
	};
	verify.config = function(obj) {
		$.each(obj, function(i, item) {
			verify.rule[i] = item
		})
	};
	$(function() {
		verify.config({
			dataRange: function(v, d) {
				var val = v.replace(/-/g, "\/"),
					min = d.getAttribute('min') ? d.getAttribute('min').replace(/-/g, "\/") : '1900/1/1',
					max = d.getAttribute('max') ? d.getAttribute('max').replace(/-/g, "\/") : '2099/12/31';

				if(new Date(val) < new Date(min) || new Date(val) > new Date(max)) {
					return "日期超出范围范围"
				}
			}
		});

		$(document).on('blur', '[blur-verify]', function() {
			if(this.getAttribute('_type') != 'date') {
				verify(this, true);
			}
		});
		$(document).on('change', '[_type="date"]', function() {
			var _this = this;
			setTimeout(function() {
				verify(_this, true);
			}, 200)
		});
	})
};